package com.techsoul.module.generator.controller;

import com.techsoul.common.core.controller.BaseController;
import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.log.annotation.Log;
import com.techsoul.common.log.enums.OperationTypeEnum;
import com.techsoul.module.generator.pojo.param.GenTableSaveParam;
import com.techsoul.module.generator.pojo.param.GenTableSearchParam;
import com.techsoul.module.generator.service.IGenTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * 代码生成表 控制层
 *
 * @author liuen
 */
@Api(tags = "代码生成接口")
@RequestMapping("/gen/genTable")
@RestController
public class GenTableController extends BaseController {

    @Autowired
    private Result result;

    @Autowired
    private IGenTableService service;

    @ApiOperation("导入数据库表结构")
    @PreAuthorize("hasAnyAuthority('gen:genTable:insert')")
    @Log(title = "代码生成", operationType = OperationTypeEnum.INSERT)
    @PostMapping("/importTable")
    public Result importTable(String tables) {
        service.importGenTable(tables);
        return result.success();
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @PreAuthorize("hasAnyAuthority('gen:genTable:delete')")
    @Log(title = "代码生成", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String[] ids) {
        return result.success(service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    @PreAuthorize("hasAnyAuthority('gen:genTable:update')")
    @Log(title = "代码生成", operationType = OperationTypeEnum.UPDATE)
    @PutMapping
    public Result update(@Validated @RequestBody GenTableSaveParam params) {
        return result.success(service.update(params));
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id) {
        return result.success(service.findById(id));
    }

    @ApiOperation("获取数据库表的字段列表")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @GetMapping("/getTableColumnList/{tableName}")
    public Result getTableColumnList(@PathVariable String tableName) {
        return result.success(service.findTableColumnList(tableName));
    }

    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody GenTableSearchParam params) {
        return result.success(service.findAll(params));
    }

    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody GenTableSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(service.findAll(params, current, pageSize));
    }

    @ApiOperation("数据库表-列表")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @PostMapping("/search/dbTableList")
    public Result findDbTableAll(@RequestBody GenTableSearchParam params) {
        return result.success(service.findDbTableAll(params));
    }

    @ApiOperation("数据库表-列表-分页")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @PostMapping("/search/dbTableList/{current}/{pageSize}")
    public Result findDbTableAll(@RequestBody GenTableSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(service.findDbTableAll(params, current, pageSize));
    }

    @ApiOperation("预览代码")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @GetMapping("/preview/{id}")
    public Result preview(@PathVariable("id") String id) {
        return result.success(service.previewCode(id));
    }

    @ApiOperation("生成代码-自定义路径，不生成SQL和前端代码")
    @PreAuthorize("hasAnyAuthority('gen:genTable:insert')")
    @Log(title = "代码生成", operationType = OperationTypeEnum.GEN_CODE)
    @GetMapping("/genCode/{tableName}")
    public Result genCode(@PathVariable("tableName") String tableName) {
        service.genCode(tableName);
        return result.success();
    }

    @ApiOperation("下载代码")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @Log(title = "代码生成", operationType = OperationTypeEnum.GEN_CODE)
    @GetMapping("/download/{tableName}")
    public void download(@PathVariable("tableName") String tableName, HttpServletResponse response) {
        service.downloadCode(tableName, response);
    }

    @ApiOperation("批量下载代码")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @Log(title = "代码生成", operationType = OperationTypeEnum.GEN_CODE)
    @GetMapping("/batchDownload/{tableNames}")
    public void batchDownload(@PathVariable("tableNames") String tableNames, HttpServletResponse response) {
        service.downloadCode(tableNames.split(","), response);
    }

    @ApiOperation("同步数据库表结构")
    @PreAuthorize("hasAnyAuthority('gen:genTable:search')")
    @GetMapping("/syncDbTable/{tableName}")
    public Result syncDbTable(@PathVariable("tableName") String tableName) {
        service.syncDbTable(tableName);
        return result.success();
    }

}
